package com.stropheware.droidDefense;

import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.graphics.Typeface;
import android.graphics.drawable.Drawable;
import android.text.TextPaint;

public class RendererUtil {

	private static Paint popUpPaint = new Paint();
	private static Paint popUpOutlinePaint = new Paint();
	private static Paint towerRadiusPaint = new Paint();
	private static final Paint popUpHealthPaint = new Paint();
	private static final TextPaint popUpTextPaint = new TextPaint();
	public static Paint dragPaint = new Paint();
	private static RectF popUp = new RectF();
	private static int tPopUpWidth = 160;
	private static int tPopUpHeight = 100;
	private static int ePopUpWidth = 130;
	private static int ePopUpHeight = 80;
	private static boolean adjusted = false;
	
	static{
		popUpPaint.setColor(Color.TRANSPARENT);
		popUpPaint.setAntiAlias(true);	
		popUpOutlinePaint.setAntiAlias(true);
		popUpOutlinePaint.setColor(Color.WHITE);
		popUpOutlinePaint.setStyle(Paint.Style.STROKE);
		popUpOutlinePaint.setStrokeWidth(2);
		popUpHealthPaint.setColor(Color.GREEN);
		popUpHealthPaint.setAntiAlias(true);
		popUpTextPaint.setAntiAlias(true);
		towerRadiusPaint.setColor(0xaa00cc00);
		towerRadiusPaint.setStrokeWidth(10);
		towerRadiusPaint.setAntiAlias(true);
		dragPaint.setColor(0xaa00cc00);
		dragPaint.setAntiAlias(true);
	}
	
	private static void adjust(float scalar){
		adjusted = true;
		tPopUpWidth *= scalar;
		tPopUpHeight *= scalar;
		ePopUpWidth *= scalar;
		ePopUpHeight *= scalar;
	}
	
	public static void drawEnemyPopUp(AbstractEnemy enemy, Canvas canvas, GameBoard board){
		if(!adjusted)
			adjust(board.densityScalar);
		float scalar = board.densityScalar;
		int x = enemy.xPixel;
		int y = enemy.yPixel;
		int width = board.actualWidth;
		int height = board.actualHeight;
		int widthPixels = board.cellSideLength * AbstractEnemy.SIZE;
		int left, right, top, bottom;
		int offset1, offset2, offset3, offset4, offset5;
		//health bar
		int hRight, hLeft, hRemaining;
		//make sure popUp is on screen
		if(x + ePopUpWidth >= width)
			left = width-ePopUpWidth-10;
		else 
			left = x;
		if(y + widthPixels + ePopUpHeight >= height)
			top = y - ePopUpHeight;
		else 
			top = y + widthPixels;
		//config rest of popUp dimensions and inner item dimensions/offsets
		offset1 = (int)(scalar*20);
		right = left + ePopUpWidth;
		bottom = top + ePopUpHeight;
		hRight = left + ePopUpWidth - offset1;
		hLeft = left + offset1;
		hRemaining = (int) ((hRight - hLeft) * enemy.healthRatio);
		
		if(enemy.displayPopUp && enemy.visible){
			//shadow layer
			popUpPaint.setShadowLayer(4, 0, 1, 0xbb000000);
			popUp.set(left, top, right, bottom);
			canvas.drawRoundRect(popUp, 7, 7, popUpPaint);
			
			//draw enemy icon
			offset1 = (int)(scalar*5);
			offset2 = (int)(scalar*30);
			offset3 = (int)(scalar*45);
			offset4 = (int)(scalar*70);
			enemy.drawable.setBounds(left + offset1, top + offset2,
									 left + offset3,top + offset4);
			enemy.drawable.draw(canvas);
			
			//draw outline - keeping this off for now
			offset1 = (int)(scalar*3);
			popUp.set(left + offset1, top + offset1, right - offset1, bottom - offset1);
			canvas.drawRoundRect(popUp, 3, 3, popUpOutlinePaint);
		
			//drawLabels
			offset1 = (int)(scalar*11);
			popUpTextPaint.setTextSize(offset1);
			popUpTextPaint.setColor(Color.WHITE);
			offset1 = (int)(scalar*50);
			offset2 = (int)(scalar*55);
			offset3 = (int)(scalar*40);
			offset4 = (int)(scalar*70);
			canvas.drawText(AbstractEnemy.NAME_LABEL, left + offset1, top + offset3, popUpTextPaint);
			canvas.drawText(AbstractEnemy.SPEED_LABEL, left + offset1, top + offset2, popUpTextPaint);
			canvas.drawText(AbstractEnemy.REWARD_LABEL, left + offset1, top + offset4, popUpTextPaint);

			//draw healthbar
			offset1 = (int)(8 * scalar);
			offset2 = (int)(21 * scalar);
			popUp.set(hLeft, top + offset1, hRight, top + offset2);
			popUpHealthPaint.setColor(Color.WHITE);
			canvas.drawRoundRect(popUp, 2, 2, popUpHealthPaint);		
			popUp.set(hLeft, top + offset1, hLeft + hRemaining, top + offset2);
			if(enemy.healthRatio >= 0.66f)
				popUpHealthPaint.setColor(Color.GREEN);
			else if(enemy.healthRatio <= 0.33f)
				popUpHealthPaint.setColor(Color.RED);
			else
				popUpHealthPaint.setColor(Color.YELLOW);
			canvas.drawRoundRect(popUp, 2, 2, popUpHealthPaint);
			
			//keep this in case we want old health bar back
//			canvas.drawText(getHealthString(), popUpLeft+54, popUpTop+18, t);
			
			//draw info
			offset1 = (int)(scalar*9);
			popUpTextPaint.setTextSize(offset1);
			offset1 = (int)(scalar*40);
			offset2 = (int)(scalar*70);
			offset3 = (int)(scalar*85);
			offset4 = (int)(scalar*90);
			offset5 = (int)(scalar*55);
			popUpTextPaint.setColor(Color.GREEN);
			canvas.drawText(enemy.name, left + offset3, top + offset1, popUpTextPaint);
			canvas.drawText(enemy.speedString, left + offset3, top + offset5, popUpTextPaint);
			canvas.drawText(enemy.rewardString, left + offset4, top + offset2, popUpTextPaint);
			
			}
	}
	
	
	public static void drawTowerRadius(AbstractTower tower, Canvas canvas) {
		canvas.drawCircle(tower.centerX, tower.centerY, tower.radius, towerRadiusPaint);
	}
	
	/**
	 * TODO: this is really messy -- could trim down these "random" offsets by using
	 *       a monospace font however may lose some aesthetic value...discuss
	 */
	public static void drawTowerPopUp(AbstractTower tower, Canvas canvas, GameBoard board){
		if(!adjusted)
			adjust(board.densityScalar);
		if(tower.display){	
			float scalar = board.densityScalar;
			int left, right, top, bottom;
			if(tower.leftPx+tPopUpWidth >= board.actualWidth)
				left = board.actualWidth-tPopUpWidth-10;
			else 
				left = tower.leftPx;
			if(tower.topPx+tower.widthPixels+tPopUpHeight >= board.actualHeight)
				top = tower.topPx-tPopUpHeight;
			else 
				top = tower.topPx+tower.widthPixels;
			
			right = left+tPopUpWidth;
			bottom = top+tPopUpHeight;
			AbstractTower.widthpx = tower.widthPixels;
			
			popUpPaint.setShadowLayer(4, 0, 1, 0xbb000000);
			popUp.set(left,top,right,bottom);
			
			
			int towerPad = (int)(5 * scalar);
			int tPopUpSize = (int)(50 * scalar);
			int tPopUpOff = (int)(95 * scalar);
			tower.upgrade.setBounds((int)left+towerPad, (int)top+towerPad, (int)left+tPopUpSize, (int)top+tPopUpSize);
			tower.sell.setBounds((int)left+towerPad, (int)top+tPopUpSize, (int)left+tPopUpSize, (int)top+tPopUpOff);
			
			canvas.drawRoundRect(popUp, 7, 7, popUpPaint);
			popUp.set(left+3,top+3,right-3,bottom-3);
			canvas.drawRoundRect(popUp, 3, 3, popUpOutlinePaint);
			TextPaint t = new TextPaint();
			t.setAntiAlias(true);
			t.setTextSize(11 * scalar);
			int textPad = (int)(55 * scalar);
			int textPad1 = (int)(86 * scalar);
			int textPad2 = (int)(96 * scalar);
			int textPad3 = (int)(92 * scalar);
			int textPad4 = (int)(20 * scalar);
			int textPad5 = (int)(102 * scalar);
			int textPad6 = (int)(32 * scalar);
			int textPad7 = (int)(44 * scalar);
			int textPad8 = (int)(75 * scalar);

	
			t.setColor(Color.WHITE);
			canvas.drawText("Damage: ", left+textPad, top+textPad4, t);
			t.setColor(0xffff4444);
			canvas.drawText(""+tower.damagePerShot+" -- 50", left+textPad5, top+textPad4, t);
			t.setColor(Color.WHITE);
			canvas.drawText("Range: ", left+textPad, top+textPad6, t);
			t.setColor(Color.GREEN);
			canvas.drawText(""+tower.radius+" -- 100", left+textPad3, top+textPad6, t);
			t.setColor(Color.WHITE);
			canvas.drawText("Cost: ", left+textPad, top+textPad7, t);
			t.setColor(Color.YELLOW);
			canvas.drawText("100g", left+textPad1, top+textPad7, t);
			t.setColor(Color.WHITE);
			canvas.drawText("Sell for: ", left+textPad, top+textPad8, t);
			t.setColor(Color.YELLOW);
			canvas.drawText("100g", left+textPad2, top+textPad8, t);
			tower.upgrade.draw(canvas);
			tower.sell.draw(canvas);
			
		}
	}

	public static void drawDragTower(Drawable drawable, Canvas canvas, GameBoard gameBoard) {
		int top = drawable.getBounds().top;
		int left = drawable.getBounds().left;
		int halfTower = gameBoard.towerWidthPx / 2;
		//Also draw radius this eventually needs to take into account 
		//specific towers initial radius, currently does not
		canvas.drawCircle(left + halfTower, top + halfTower, AbstractTower.defaultRadius * gameBoard.cellSideLength, towerRadiusPaint);
		drawable.draw(canvas);
		canvas.drawRect(left, top, left + gameBoard.towerWidthPx, top + gameBoard.towerWidthPx, dragPaint);
	}
	
	
}
